import csv
import random
from datetime import datetime
from time import strftime
#from dbfpy import dbf
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey


#--------------------------------------------------------------------------
#MAIN
if __name__ == "__main__":
    print "begin at " + getCurTime()
    inputCSV = 'C:/_DATA/CancerData/test/Jun08/total.csv'
    data = build_data_list(inputCSV)
    
    # Q-Q plot
    values = data[:,1]
    fig = plt.figure()                 # set up plot
    ax = fig.add_subplot(1, 1, 1)
    (osm, osr), (m, b, r) = st.probplot(values, dist='norm')  # compute
    osmf = osm.take([0, -1])  # endpoints
    osrf = m * osmf + b       # fit line
    plt.title('Normal Q-Q Plot')
    plt.ylabel('Precipitation Quantiles')
    plt.xlabel('Normal Theoretical Quantiles')
    ax.plot(osm, osr, '.', osmf, osrf, '-')
    
    plt.show()
    